Java syntax
part 9/23 Β· 86.0 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Labels can be specified in continue statements and break statements:
outer:
for (String str : stringsArr) {
char[] strChars = str.toCharArray();
for (char ch : strChars) {
if (ch == ' ') {
/* Continues the outer cycle and the next
string is retrieved from stringsArr */
continue outer;
}
doSomething(ch);
}
}
return statement
The return statement is used to end method execution and to return a
value. A value returned by the method is written after the return
keyword. If the method returns anything but void, it must use the return
statement to return some value.
void doSomething(boolean streamClosed) {
// If streamClosed is true, execution is stopped
if (streamClosed) {
return;
}
readFromStream();
}
int calculateSum(int a, int b) {
int result = a + b;
return result;
}
return statement ends execution immediately, except for one case: if the
statement is encountered within a try block and it is complemented by a
finally, control is passed to the finally block.
void doSomething(boolean streamClosed) {
try {
if (streamClosed) {
return;
}
readFromStream();
} finally {
/* Will be called last even if
readFromStream() was not called */
freeResources();
}
}
Exception handling statements
try-catch-finally statements
Exceptions are managed within try ... catch blocks.
try {
// Statements that may throw exceptions
methodThrowingExceptions();
} catch (Exception ex) {
// Exception caught and handled here
reportException(ex);
} finally {
// Statements always executed after the try/catch blocks
freeResources();
}
The statements within the try block are executed, and if any of them
throws an exception, execution of the block is discontinued and the
exception is handled by the catch block. There may be multiple catch
blocks, in which case the first block with an exception variable whose
type matches the type of the thrown exception is executed.
Java SE 7 also introduced multi-catch clauses besides uni-catch clauses.
This type of catch clauses allows Java to handle different types of
exceptions in a single block provided they are not subclasses of each
other.
try {
methodThrowingExceptions();
} catch (IOException | IllegalArgumentException ex) {
//Both IOException and IllegalArgumentException will be caught and
handled here
reportException(ex);
}
If no catch block matches the type of the thrown exception, the
execution of the outer block (or method) containing the try ... catch
statement is discontinued, and the exception is passed up and outside
the containing block (or method). The exception is propagated upwards
through the call stack until a matching catch block is found within one
of the currently active methods. If the exception propagates all the way
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ